﻿using System;
using System.Collections.Generic;
using System.Text;
using Lswweb.Model;
using Lswweb.Domain;
using Lswweb.Common;
using System.Web.UI.WebControls;

namespace Lswweb.Controls
{
    public class CurrencyEdit : SkinnedEditControl<Currency>
    {
        private TextBox tbxName;
        private TextBox tbxSymbol;
        private TextBox tbxExchangeRate;
        private TextBox tbxCode;
        private CheckBox cbxIsDefault;
        private ImageUpload fileIcon;
        private Image imgPreview;

        protected override void InitSkin(System.Web.UI.Control skin)
        {
            tbxName = skin.FindControl<TextBox>("tbxName");
            tbxSymbol = skin.FindControl<TextBox>("tbxSymbol");
            tbxExchangeRate = skin.FindControl<TextBox>("tbxExchangeRate");
            tbxCode = skin.FindControl<TextBox>("tbxCode");
            fileIcon = skin.FindControl<ImageUpload>("fileIcon");
            cbxIsDefault = skin.FindControl<CheckBox>("cbxIsDefault");

            imgPreview = skin.FindControl<Image>("imgPreview");
            imgPreview.Visible = false;
        }

        private bool UploadFile(Currency model)
        {
            if (fileIcon.HasFile)
            {
                model.Icon = fileIcon.SaveAs();
                if (string.IsNullOrEmpty(model.Icon))
                    return false;
            }

            return true;
        }

        protected override void Add(Currency model)
        {
            if (!UploadFile(model))
                return;

            CurrencyServices.Insert(model);            
        }

        protected override void Update(Currency model)
        {
            if (!UploadFile(model))
                return;

            CurrencyServices.Update(model);            
        }

        protected override Currency FindById(int id)
        {
            return CurrencyServices.GetCurrency(id);
        }

        protected override Currency ModelByFrom
        {
            get
            {
                Currency entity = new Currency();

                entity.Name = tbxName.GetValue<String>(); 
                entity.Symbol =tbxSymbol.GetValue<String>();
                entity.Code = tbxCode.GetValue<string>();
                entity.IsDefault = cbxIsDefault.Checked;
                entity.ExchangeRate = tbxExchangeRate.GetValue<decimal>();

                return entity;
            }
            set
            {
                tbxName.SetValue(value.Name);
                tbxSymbol.SetValue(value.Symbol);
                cbxIsDefault.Checked = value.IsDefault;
                tbxCode.SetValue(value.Code);
                tbxExchangeRate.SetValue(value.ExchangeRate);

                if (!string.IsNullOrEmpty(value.Icon))
                {
                    imgPreview.Visible = true;
                    imgPreview.ImageUrl = value.Icon;
                }

                if (value.IsDefault)
                {
                    tbxExchangeRate.Enabled = false;
                }
            }
        }

        protected override string DefaultSkinFilename
        {
            get { return "Currency/CurrencyEdit.ascx"; }
        }
    }
}
